//
//  ProtocolStream.m
//  Protocol
//
//  Created by  user on 11-1-10.
//  Copyright 2011 GuangZhouXuanWu. All rights reserved.
//

#import "ProtocolStream.h"


@implementation CProtocolStream

@synthesize stringEncoding = _stringEncoding;

-(id)init
{
	self = [super init];
	_stringEncoding=2;
	return self;
}

-(void)writeInt:(int)nDight
{
	[CBitConvert toIntArray:nDight inoutput:self output:&_position];
}

-(void)writeNumberInt:(NSNumber*)nNum
{
    [self writeInt:[nNum intValue]];
}

-(void)writeidToInt:(id)data length:(NSNumber*)nLength
{
	int *n=(int*)data;
	int nLen=[nLength intValue];
	for(int i=0;i<nLen;i++)
	{
		[self writeInt:n[i]];
	}
}

-(int)readInt
{
	return [CBitConvert fromIntArray:self output:&_position];
}

-(NSNumber*)readNumberInt
{
    return [NSNumber numberWithInt:[self readInt]];
}

-(void)writeShort:(short)nDight
{
	[CBitConvert toShortArray:nDight inoutput:self output:&_position];
}

-(short)readShort
{
	return [CBitConvert fromShortArray:self output:&_position];
}

-(void)writeChar:(char)ch
{
	[CBitConvert toCharArray:ch inoutput:self output:&_position];
}

-(char)readChar
{
	return [CBitConvert fromCharArray:self output:&_position];
}

-(void)writeIDToChar:(id)data length:(NSNumber*)nLength
{	
	char *n=(char*)data;
	int nLen=[nLength intValue];
	for(int i=0;i<nLen;i++)
	{
		[self writeChar:n[i]];
	}
}

-(void)writeGUID:(GUID)guid
{
	[CBitConvert toGUIDArray:guid inoutput:self output:&_position];
}

-(void)writeGUIDByCGUID:(XMSGUID*)guid
{
    [self writeGUID:guid.guid];
}

-(void)writeIDToGUID:(id)data length:(NSNumber*)nLength
{	
	GUID *n=(GUID*)data;
	int nLen=[nLength intValue];
	for(int i=0;i<nLen;i++)
	{
		[self writeGUID:n[i]];
	}
}

-(GUID)readGUID
{
	return [CBitConvert fromGUIDArray:self output:&_position];
}

-(void)readGUIDs:(CProtocolStream*)ps output:(NSMutableArray**)objs
{
	int nCount=[ps readInt];
	if(nCount>0)
	{
		(*objs)=[[NSMutableArray alloc]initWithCapacity:nCount];
		for(int i=0;i<nCount;i++)
		{
			XMSGUID* guid=[XMSGUID new];
			guid.guid=[ps readGUID];
			[(*objs) addObject:guid];
			[guid release_mm];
		}
	}
	else 
		[ps setPosition:[ps position]+1];
}

-(void)writeString:(NSString*)str
{
	[CBitConvert toStringArray:str inoutput:self output:&_position];
}

-(NSString*)readString
{
	return [CBitConvert fromStringArray:_stringEncoding buffer:self output:&_position];
}

-(void)writeLongLong:(long long)nDight
{
	[CBitConvert toLongLongArray:nDight inoutput:self output:&_position];
}

-(long long)readLongLong
{
	return [CBitConvert fromLongLongArray:self output:&_position];
}

-(void)writeDateTime:(NSDate*)datetime
{
	[CBitConvert toDateTimeArray:datetime inoutput:self output:&_position];
}

-(NSDate*)readDateTime
{
	return [CBitConvert fromDateTimeArray:self output:&_position];
}

-(void)toArrays:(id)data dataLength:(int)nLen target:(CProtocolStream*)ps action:(SEL)action
{
	if(nil!=data)
	{
		[self writeInt:nLen];
		if(nLen>0)
		{
			[ps performSelector:action withObject:data withObject:[NSNumber numberWithInt:nLen]];
		}
		else
		{
			[self writeChar:1];
		}
	}
	else
	{
		[self writeInt:0];
		[self writeChar:0];
	}
}

-(void)toNSArrayObjArrays:(NSArray*)arObjs keyName:(NSString*)szName target:(CProtocolStream*)ps action:(SEL)action
{
	if(nil!=arObjs)
	{
		int nLen = (int)arObjs.count;
		[self writeInt:nLen];
		if(nLen>0)
		{
			for(int i=0;i<nLen;i++)
				[ps performSelector:action withObject:[[arObjs objectAtIndex:i] valueForKey:szName]];
		}
		else
		{
			[self writeChar:1];
		}
	}
	else
	{
		[self writeInt:0];
		[self writeChar:0];
	}
}

-(void)toNSArrayObjArrays:(NSArray*)arObjs target:(CProtocolStream*)ps action:(SEL)action
{
	if(nil!=arObjs)
	{
		int nLen = (int)arObjs.count;
		[self writeInt:nLen];
		if(nLen>0)
		{
			for(int i=0;i<nLen;i++)
				[ps performSelector:action withObject:[arObjs objectAtIndex:i]];
		}
		else
		{
			[self writeChar:1];
		}
	}
	else
	{
		[self writeInt:0];
		[self writeChar:0];
	}
}

-(void)fromArraysNSArrayObj:(CProtocolStream*)ps action:(SEL)action output:(NSMutableArray**)objs
{
	int nCount=[ps readInt];
	if(nCount>0)
	{
		(*objs)=[[NSMutableArray alloc]initWithCapacity:nCount];
		for(int i=0;i<nCount;i++)
			[(*objs) addObject:[ps performSelector:action]];
	}
	else 
		[ps setPosition:[ps position]+1];
}

-(NSData*)readData
{
	return [CBitConvert fromDataArray:self output:&_position];
}

-(void)writeData:(NSData*)data
{
	[CBitConvert toDataArray:data inoutput:self output:&_position];
}

@end
